# LocalStack Resource Provider Scaffolding {{ scaffolding_version }}
from __future__ import annotations

from pathlib import Path
from typing import Optional, TypedDict

import localstack.services.cloudformation.provider_utils as util
from localstack.services.cloudformation.resource_provider import (
    OperationStatus,
    ProgressEvent,
    ResourceProvider,
    ResourceRequest,
)

{{ provider_properties }}


REPEATED_INVOCATION = "repeated_invocation"

class {{ resource }}Provider(ResourceProvider[{{ resource }}Properties]):

    TYPE = "{{ name }}"  # Autogenerated. Don't change
    SCHEMA = util.get_schema_path(Path(__file__))  # Autogenerated. Don't change

    def create(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Create a new resource.

        {% if primary_identifier -%}
        Primary identifier fields:
        {%- for property in primary_identifier %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if required_properties -%}
        Required properties:
        {%- for property in required_properties %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if create_only_properties -%}
        Create-only properties:
        {%- for property in create_only_properties %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if read_only_properties -%}
        Read-only properties:
        {%- for property in read_only_properties %}
          - {{ property }}
        {%- endfor %}
        {%- endif %}

        {% if create_permissions -%}
        IAM permissions required:
        {%- for permission in create_permissions %}
          - {{ permission }}
        {%- endfor -%}
        {%- endif %}

        """
        model = request.desired_state

        # TODO: validations

        if not request.custom_context.get(REPEATED_INVOCATION):
            # this is the first time this callback is invoked
            # TODO: defaults
            # TODO: idempotency
            # TODO: actually create the resource
            request.custom_context[REPEATED_INVOCATION] = True
            return ProgressEvent(
                status=OperationStatus.IN_PROGRESS,
                resource_model=model,
                custom_context=request.custom_context,
            )

        # TODO: check the status of the resource
        # - if finished, update the model with all fields and return success event:
        #   return ProgressEvent(status=OperationStatus.SUCCESS, resource_model=model)
        # - else
        #   return ProgressEvent(status=OperationStatus.IN_PROGRESS, resource_model=model)

        raise NotImplementedError

    def read(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Fetch resource information

        {% if read_permissions -%}
        IAM permissions required:
        {%- for permission in read_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError

    def delete(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Delete a resource

        {% if delete_permissions -%}
        IAM permissions required:
        {%- for permission in delete_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError

    def update(
        self,
        request: ResourceRequest[{{ resource }}Properties],
    ) -> ProgressEvent[{{ resource }}Properties]:
        """
        Update a resource

        {% if update_permissions -%}
        IAM permissions required:
        {%- for permission in update_permissions %}
          - {{ permission }}
        {%- endfor %}
        {%- endif %}
        """
        raise NotImplementedError
